import { reactive } from "vue";
import model from "./model";
import { copyModel, formatDateTimes } from "@/utils/index";
import { $getPlantingPlan } from "@/utils/api";
const MODEL = copyModel(model);
export const getMessage = async () => {
  model.start = formatDateTimes(model.start);
  model.end = formatDateTimes(model.end);
  if (Array.isArray(model.facilities_id)) {
    model.facilities_id = model.facilities_id[1];
  }
  const res = await $getPlantingPlan(model);
  console.log(res)
  options.total = res.results.total;
  options.options.tableData = res.results.data.map((item) => {
    return {
      ...item,
      ...item.p,
      ActualEndTime: formatDateTimes(item.p?.ActualEndTime),
    };
  });
  console.log(options.options.tableData)
};
getMessage();
export const options = reactive({
  className: "kdcrud",
  config: {
    labelWidth: "120px",
    labelPosition: "left",
    formRef: "formLogin",
    formItemArr: [
      {
        id: 1,
        name: "datePicker",
        options: {
          modelName: "start",
          placeholder: "开始日期",
          size: "small",
        },
      },
      {
        id: 2,
        name: "datePicker",
        options: {
          modelName: "end",
          placeholder: "结束日期",
          size: "small",
        },
      },
      {
        id: 3,
        name: "input",
        options: {
          modelName: "historyplan_id",
          optionArr: [],
          placeholder: "请输入追溯源码",
          size: "small",
        },
      },
      {
        id: 4,
        name: "input",
        options: {
          modelName: "historyplan_type",
          optionArr: [],
          placeholder: "请输入品种",
          size: "small",
        },
      },
      {
        id: 9,
        name: "button",
        options: {
          buttonText: "搜索",
          onclick: async () => {
            getMessage();
          },
        },
      },
      {
        id: 10,
        name: "button",
        options: {
          buttonText: "重置",
          onclick: () => {
            Object.assign(model, MODEL);
          },
        },
      },
    ],
  },
  options: {
    tableData: [],
    height: 500,
    size: "small",
    border: true,
    optionsArr: [
      {
        prop: "TraceabilityCode",
        label: "追溯码",
        width: "160",
        fixed: true,
      },
      {
        prop: "facility_name",
        label: "设施",
        width: "200",
        fixed: true,
      },
      {
        prop: "CropVarieties",
        label: "品种",
        width: "100",
      },
      {
        prop: "scheme_name",
        label: "种植方案",
      },
      {
        prop: "name",
        label: "计划",
      },
      {
        prop: "PlantingStartTime",
        label: "开始时间",
        width: "120",
      },
      {
        prop: "ActualEndTime",
        label: "结束时间",
        width: "120",
      },
      {
        label: "操作",
        width: "80",
        slot: () =>
          `<button class="el-button el-button--primary el-button--" type="text">详细</button>`,
      },
    ],
  },
  background: true,
  layout: "prev, pager, next",
  total: 3500,
  currentChange(e) {
    console.log(e);
    model.pageIndex = e;
    getMessage();
  },
});

